//
//  PlusCutView.m
//  YHCustomer
//
//  Created by lichentao on 14-5-2.
//  Copyright (c) 2014年 富基融通. All rights reserved.
//

#import "PlusCutView.h"

@implementation PlusCutView
@synthesize numLabel;
@synthesize plusBlock,cutBlock;
@synthesize goodsEntity1;


- (id)initWithFrame:(CGRect)frame GoodsEntity:(NSString *)goodsEntity setPlusBlock:(PlusButtonBlock)plusBlock1 CutButtonBlock:(CutButtonBlock)cutBlock1
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
      
    
    }
    return self;
}

- (void)setFrame:(CGRect)frame GoodsEntity:(NSString *)goodsEntity setPlusBlock:(PlusButtonBlock)plusBlock1 CutButtonBlock:(CutButtonBlock)cutBlock1{
    self.plusBlock = plusBlock1;
    self.cutBlock = cutBlock1;
    self.goodsEntity1 = goodsEntity;
    //        self.backgroundColor = [UIColor grayColor];
    numLabel = [[UILabel alloc] initWithFrame:CGRectMake(31, 0, 30, 24)];
    numLabel.text = [NSString stringWithFormat:@"%d",1];
    numLabel.textAlignment = NSTextAlignmentCenter;
    numLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
    numLabel.layer.borderWidth = 1.0f;
    
    plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
    plusButton.backgroundColor = [UIColor clearColor];
    plusButton.frame = CGRectMake(69, 0, 24, 24);
    plusButton.tag = 1001;
    plusButton.userInteractionEnabled = YES;
    [plusButton setBackgroundImage:[UIImage imageNamed:@"ps_plus.png"] forState:UIControlStateNormal];
    [plusButton setBackgroundImage:[UIImage imageNamed:@"ps_plus_Select.png"] forState:UIControlStateHighlighted];
    [plusButton setBackgroundImage:[UIImage imageNamed:@"ps_plus_Select.png"] forState:UIControlStateSelected];
    [plusButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    cutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cutButton.frame = CGRectMake(0, 0, 24, 24);
    cutButton.backgroundColor = [UIColor clearColor];
    cutButton.tag = 1002;
    cutButton.userInteractionEnabled = YES;
    [cutButton setBackgroundImage:[UIImage imageNamed:@"ps_cut.png"] forState:UIControlStateNormal];
    [cutButton setBackgroundImage:[UIImage imageNamed:@"ps_cut_Select.png"] forState:UIControlStateHighlighted];
    [cutButton setBackgroundImage:[UIImage imageNamed:@"ps_cut_Select.png"] forState:UIControlStateSelected];
    [cutButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:numLabel];
    [self addSubview:cutButton];
    [self addSubview:plusButton];
}

- (void)buttonClicked:(id)sender{
    UIButton *btn = (UIButton *)sender;
    if (btn.tag == 1001) {// PLUS
        int maxNum = [goodsEntity1 intValue];
        if ([numLabel.text intValue] == maxNum) {
            [plusButton setBackgroundImage:[UIImage imageNamed:@"ps_plus_Select.png"] forState:UIControlStateNormal];
            return;
        }
        [plusButton setBackgroundImage:[UIImage imageNamed:@"ps_plus.png"] forState:UIControlStateNormal];

        numLabel.text = [NSString stringWithFormat:@"%d",[numLabel.text intValue]+1];
        plusBlock(numLabel.text);
    }else{                // cut
        if ([numLabel.text intValue] == 1) {
            [cutButton setBackgroundImage:[UIImage imageNamed:@"ps_cut_Select.png"] forState:UIControlStateNormal];
            return;
        }
        [cutButton setBackgroundImage:[UIImage imageNamed:@"ps_cut.png"] forState:UIControlStateNormal];
        numLabel.text = [NSString stringWithFormat:@"%ld",[numLabel.text integerValue]-1];
        cutBlock(numLabel.text);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
